﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw.UserService;
using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class PageDesigner : UserControl
    {
        public const int DESIGN_ARROW = 0;
        public const int DESIGN_TEXT = 1;
        public const int DESIGN_IMAGE = 2;
        public const int DESIGN_DELETE = 3;

        internal WidgetServiceProxy ServiceProxy;
        internal WidgetSpaceProxy SpaceProxy;
        private PageDesign m_design;
        private int m_controlType;
        public PageDesigner()
        {
            InitializeComponent();
        }

        private void Refresh()
        {
            if (m_design == null) return;

            if (m_design.Texts != null)
            {
                // add control
                while (TextRoot.Children.Count < m_design.Texts.Count)
                {
                    TextRoot.Children.Add(new PageTextInstance());
                }
                // hide unused
                for (int i = TextRoot.Children.Count; i > m_design.Texts.Count; i--)
                {
                    TextRoot.Children[i].Visibility = Visibility.Collapsed;
                }
                //modify
                for (int i = 0; i < m_design.Texts.Count; i++)
                {
                    PageTextInstance pti = TextRoot.Children[i] as PageTextInstance;
                    pti.PageText = m_design.Texts[i];
                    pti.Visibility = Visibility.Visible;
                }
            }

            if (m_design.Images != null)
            {
                // add control
                while (ImageRoot.Children.Count < m_design.Images.Count)
                {
                    ImageRoot.Children.Add(new PageImageInstance());
                }
                // hide unused
                for (int i = ImageRoot.Children.Count; i > m_design.Images.Count; i--)
                {
                    ImageRoot.Children[i-1].Visibility = Visibility.Collapsed;
                }
                //modify
                for (int i = 0; i < m_design.Images.Count; i++)
                {
                    PageImageInstance pii = (PageImageInstance)ImageRoot.Children[i];
                    pii.PageImage = m_design.Images[i];
                    pii.Visibility = Visibility.Visible;
                }
            }
        }

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))// no session at all
                return;
            XmlSerializer xs = new XmlSerializer(typeof(PageDesign));
            XmlReader xr = XmlReader.Create(new StringReader(e.Result));
            PageDesign data = (PageDesign)xs.Deserialize(xr);
            m_design = data;
            Refresh();
        }

        void OnSetSession(object sender, DragDraw.UserService.SetSessionCompletedEventArgs e)
        {
            if(e.Result == false)
            {
                ErrorManager.Instance.ShowLastServiceError();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceProxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            ServiceProxy.GetSessionCompleted += OnGetSession;
            ServiceProxy.SetSessionCompleted += OnSetSession;
            SpaceProxy = OperationManager.Instance.CreateWidgetSpaceProxy(this);

            ServiceProxy.GetSession("pagedesign");

            if (OperationManager.Instance.IsInMySpace) ;
                //BtnMgr.Visibility = Visibility.Visible;
        }

        private void BtnArrow_Click(object sender, RoutedEventArgs e)
        {
            m_controlType = DESIGN_ARROW;
        }

        private void BtnText_Click(object sender, RoutedEventArgs e)
        {
            m_controlType = DESIGN_TEXT;
        }

        private void BtnImage_Click(object sender, RoutedEventArgs e)
        {
            m_controlType = DESIGN_IMAGE;
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            m_controlType = DESIGN_DELETE;
        }

        private void TextRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_controlType == DESIGN_TEXT)
            {
                PageText pt = new PageText();
                pt.Content = "";
                pt.Height = 1;
                pt.Width = 2;
                pt.PosX = 1;
                pt.PosY = 2;
                m_design.Texts.Add(pt);
                Refresh();
                m_controlType = DESIGN_ARROW;
            }
            else if(m_controlType == DESIGN_ARROW)
            {
                foreach(UIElement pti in TextRoot.Children)
                {
                    //if(pti.HitTest(e.GetPosition(null)) != null)
                    {
                    }
                }
            }
            else if (m_controlType == DESIGN_DELETE)
            {
            }
        }

        private void ImageRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_controlType == DESIGN_IMAGE)
            {
                PageImage pi = new PageImage();
                pi.ImageUrl = "";
                pi.Height = 1;
                pi.Width = 2;
                pi.PosX = 1;
                pi.PosY = 2;
            }
        }
    }

    public class PageDesign
    {
        public List<PageText> Texts { get; set; }
        public List<PageImage> Images { get; set; }
    }

    

    
}
